//package org.zbclan.bf2.rcon.query;
//
//import java.io.File;
//import java.io.IOException;
//import java.util.Enumeration;
//import java.util.Iterator;
//import java.util.List;
//
//import javax.servlet.ServletException;
//import javax.servlet.http.HttpServlet;
//import javax.servlet.http.HttpServletRequest;
//import javax.servlet.http.HttpServletResponse;
//import javax.servlet.http.HttpSession;
//
//import org.apache.commons.fileupload.FileItem;
//import org.apache.commons.fileupload.FileUploadException;
//import org.apache.commons.fileupload.disk.DiskFileItemFactory;
//import org.apache.commons.fileupload.servlet.FileCleanerCleanup;
//import org.apache.commons.fileupload.servlet.ServletFileUpload;
//import org.apache.commons.io.FileCleaningTracker;
//
//public class FileUploadServlet extends HttpServlet {
//
//	/**
//	 * 
//	 */
//	private static final long serialVersionUID = -6019642623115994000L;
//
//	private HttpSession session;
//
//	@Override
//	protected void service(HttpServletRequest req, HttpServletResponse resp)
//			throws IOException, ServletException {
//
//		session = req.getSession();
//
//		@SuppressWarnings("unchecked")
//		Enumeration<String> names = req.getParameterNames();
//		while (names.hasMoreElements()) {
//			String p = (String) names.nextElement();
//			for (String v : req.getParameterValues(p)) {
//				System.out.println(p + " = " + v);
//			}
//		}
//
//		super.service(req, resp);
//
//		return;
//	}
//
//	@Override
//	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
//			throws ServletException, IOException {
//
//		/* Check that we have a file upload request */
//		boolean isMultipart = ServletFileUpload.isMultipartContent(req);
//		if (!isMultipart) {
//			resp.sendError(HttpServletResponse.SC_BAD_REQUEST,
//					"multipart expected");
//			return;
//		}
//
//		int yourMaxMemorySize = 1024 * 1024;
//		File yourTempDirectory = null;
//		long yourMaxRequestSize = 1024 * 1024 * 1024;
//
//		FileCleaningTracker fileCleaningTracker = FileCleanerCleanup
//				.getFileCleaningTracker(getServletContext());
//
//		/* Create a factory for disk-based file items */
//		DiskFileItemFactory factory = new DiskFileItemFactory(
//				yourMaxMemorySize, yourTempDirectory);
//		factory.setFileCleaningTracker(fileCleaningTracker);
//
//		/* Create a new file upload handler */
//		ServletFileUpload upload = new ServletFileUpload(factory);
//
//		/* Set overall request size constraint */
//		upload.setSizeMax(yourMaxRequestSize);
//
//		/* Parse the request */
//		try {
//			@SuppressWarnings("unchecked")
//			List<FileItem> items = upload.parseRequest(req);
//
//			// Process the uploaded items
//			Iterator<FileItem> iter = items.iterator();
//			while (iter.hasNext()) {
//				FileItem item = iter.next();
//
//				if (item.isFormField()) {
//					processFormField(item);
//				} else {
//					processUploadedFile(item);
//				}
//			}
//		} catch (FileUploadException ex) {
//			throw new ServletException("upload error", ex);
//		}
//
//		return;
//	}
//
//	private void processUploadedFile(FileItem item) {
//
//		/* Process a file upload */
//		if (!item.isFormField()) {
//			String fieldName = item.getFieldName();
//			String fileName = item.getName();
//			String contentType = item.getContentType();
//			boolean isInMemory = item.isInMemory();
//			long sizeInBytes = item.getSize();
//			/* ... */
//			// Process a file upload
//			boolean writeToFile = false;
//			if (writeToFile) {
//				// File uploadedFile = new File(...);
//				// item.write(uploadedFile);
//			} else {
//				// InputStream uploadedStream = item.getInputStream();
//				// /* ... */
//				// uploadedStream.close();
//
//				/* Process a file upload in memory */
//				byte[] data = item.get();
//			}
//		}
//
//		return;
//	}
//
//	private void processFormField(FileItem item) {
//
//		/* Process a regular form field */
//		if (item.isFormField()) {
//			String name = item.getFieldName();
//			String value = item.getString();
//			/* ... */
//		}
//	}
//}
